app.controller("z-salaryRequest-c", ['$scope', '$rootScope', '$z-http', '$sce', 'z-salaryRequest-s','z-salaryDetail-s', 'z-dictionary-s', 'z-corporation-s', 'z-department-s', 'z-edit-menu-s', '$filter',
    function ($scope, $rootScope, $zhttp, $sce, Service, SalaryDetailService, DictionaryService, CorporationService, DepartmentService, MenuService, $filter) {
        $scope.viewModel = {};
        $scope.viewModel.perPage = "10";
        $scope.viewModel.rowCount = "1";
        //查询页面名称
        MenuService.getMenuByUrl({url: 'salaryRequest'},
            function (data) {
                $scope.viewModel.menuList = data;
            }, function (error) {
            });
        // 获取本月第一天
        $scope.viewModel.reportStratDate = (new Date()).setDate(1);
        // 获取当前日期
        $scope.viewModel.reportEndDate = new Date();

        //日期控件
        $scope.viewModel.dateOptions = {
            formatYear: 'yy',
            startingDay: 1,
            showWeeks: false
        };
        //日期控件
        $scope.viewModel.format = "yyyy/MM/dd";
        //日期启动开关
        $scope.viewModel.startDateOpened = false;
        $scope.viewModel.endDateOpened = false;
        //起止日期选择
        $scope.viewModel.startDateOpen = function ($event) {
            $event.preventDefault();
            $event.stopPropagation();
            $scope.viewModel.startDateOpened = !$scope.viewModel.startDateOpened;
        };
        //起止日期选择
        $scope.viewModel.endDateOpen = function ($event) {
            $event.preventDefault();
            $event.stopPropagation();
            $scope.viewModel.endDateOpened = !$scope.viewModel.endDateOpened;
        };

        $scope.viewModel.roleName = null;
        $scope.viewModel.level = null;

        //验证
        $scope.viewModel.requiredForm = function () {
            if ($scope.viewModel.reportStratDate == "" || $scope.viewModel.reportStratDate == null) {
                $rootScope.alertWarning("开始时间不能为空。");
                return true;
            } else if ($scope.viewModel.reportEndDate == "" || $scope.viewModel.reportEndDate == null) {
                $rootScope.alertWarning("结束时间不能为空。");
            }
        };


        //加载数据
        $scope.viewModel.bindData = function () {
            if ($scope.viewModel.requiredForm()) {
                return;
            }

            $scope.viewModel.isLoading = true;

            //获取基本信息
            Service.getSalaryRequestList({
                    pageNo: $scope.viewModel.rowCount,
                    pageCount: $scope.viewModel.perPage,
                    selStartDate: $scope.viewModel.reportStratDate,
                    selEndDate: $scope.viewModel.reportEndDate,
                },
                function (data) {
                    $scope.viewModel.dataList = data.salaryRequestBeanList;
                    $scope.viewModel.totalData = data.pageTotal;
                    $scope.viewModel.isLoading = false;
                }, function (error) {
                    $scope.viewModel.isLoading = false;
                });
        };
        $scope.viewModel.bindData();

        //提交
        $scope.viewModel.saveSubmit = function(row){
            $rootScope.alertConfirm("确定提交吗？",function() {
                $scope.viewModel.isLoading = true;
                Service.saveSalaryRequestAgain({
                    requestId: row.salaryRequestId
                }, function () {
                    $scope.viewModel.bindData();
                    $scope.viewModel.isLoading = false;
                    $rootScope.alertInfo("提交成功。");
                }, function (error) {
                    $scope.viewModel.isLoading = false;
                    $rootScope.alertError(error.fault.message);
                });
            });
        };

        $scope.viewModel.withdraw = function (row) {
            $rootScope.alertConfirm("确定撤回吗？", function () {
                $scope.viewModel.isLoading = true;
                Service.getBackSalaryRequest({
                    requestId: row.salaryRequestId
                }, function () {
                    $scope.viewModel.bindData();
                    $scope.viewModel.isLoading = false;
                    $rootScope.alertInfo("撤回成功。");
                }, function (error) {
                    $scope.viewModel.isLoading = false;
                    $rootScope.alertError(error.fault.message);
                });
            });
        };

        $scope.viewModel.delete = function (row) {
            $rootScope.alertConfirm("确定删除吗？", function () {
                $scope.viewModel.isLoading = true;
                Service.deleteSalaryRequest({
                    requestId: row.salaryRequestId
                }, function () {
                    $scope.viewModel.bindData();
                    $scope.viewModel.isLoading = false;
                    $rootScope.alertInfo("删除成功。");
                }, function (error) {
                    $scope.viewModel.isLoading = false;
                    $rootScope.alertError(error.fault.message);
                });
            });
        };

        $scope.approvalRecord = {};
        $scope.viewModel.selectApprovalRecord = function (row) {
            if (row.approvalState != 0) {
                $scope.approvalRecord.show = true;
                $scope.viewModel.isLoading = true;
                $scope.approvalRecord.title = "查看审批过程";
                $scope.approvalRecord.dataList = null;
                //获取基本信息
                Service.getSalayApprovalRecordList({
                        salaryRequestId: row.salaryRequestId,
                        salaryRuleId: row.salaryRuleId
                    },
                    function (data) {
                        $scope.approvalRecord.dataList = data;
                        $scope.viewModel.isLoading = false;
                    }, function (error) {
                        $rootScope.alertError(error.fault.message);
                        $scope.viewModel.isLoading = false;
                    });
            }
        };
        //关闭加载
        $scope.viewModel.unLoad=function(){
            $scope.approvalRecord.show=false;
            $scope.detailModel.show = false;
        };

        // 显示明细
        $scope.detailModel = {
            title: "明细"
        };
        $scope.viewModel.showDetail = function (row) {
            window.open("#/salaryDetail?salaryRequestId="+row.salaryRequestId);
        };
        //下载报表
        $scope.viewModel.download = function (row) {
            $scope.viewModel.isLoading = true;
            Service.getDownloadSalary({
            	salaryRequestId: row.salaryRequestId
            }, function (data) {
                $scope.viewModel.isLoading = false;
                window.location.href = data;
            }, function (error) {
                $scope.viewModel.isLoading = false;
                $rootScope.alertError(error.fault.message);
            });
        };

        $scope.viewModel.ulShow = false;
        // 设置初始值
        $scope.viewModel.checkbox1 = true;
        $scope.viewModel.checkbox2 = true;
        $scope.viewModel.checkbox3 = true;
        $scope.viewModel.checkbox4 = true;
        $scope.viewModel.checkbox5 = true;
        $scope.viewModel.checkbox6 = true;


    }
]);